package com.youyi.yyviewsdklibrary;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import java.io.File;
import java.text.DateFormat;
import java.util.Objects;

public class PackageUtils {
    private static ApplicationInfo getApplicationInfo(String str, Context context) {
        try {
            return getPackageManager(context).getApplicationInfo(str, 128);
        } catch (Exception unused) {
            return null;
        }
    }

    public static boolean isEnabled(String str, Context context) {
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return applicationInfo.enabled;
    }

    public static boolean isPackageInstalled(String str, Context context) {
        try {
            getPackageManager(context).getApplicationInfo(str, 0);
            return true;
        } catch (PackageManager.NameNotFoundException unused) {
            return false;
        }
    }

    public static boolean isSystemApp(String str, Context context) {
        try {
            ApplicationInfo applicationInfo = getApplicationInfo(str, context);
            Objects.requireNonNull(applicationInfo);
            ApplicationInfo applicationInfo2 = applicationInfo;
            return (applicationInfo.flags & 1) != 0;
        } catch (NullPointerException unused) {
            return false;
        }
    }

    public static boolean isUpdatedSystemApp(String str, Context context) {
        try {
            ApplicationInfo applicationInfo = getApplicationInfo(str, context);
            Objects.requireNonNull(applicationInfo);
            ApplicationInfo applicationInfo2 = applicationInfo;
            return (applicationInfo.flags & 128) != 0;
        } catch (NullPointerException unused) {
            return false;
        }
    }

    public static CharSequence getAppName(String str, Context context) {
        PackageManager packageManager = getPackageManager(context);
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return packageManager.getApplicationLabel(applicationInfo);
    }

    public static Drawable getAppIcon(String str, Context context) {
        PackageManager packageManager = getPackageManager(context);
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return packageManager.getApplicationIcon(applicationInfo);
    }

    public static PackageInfo getPackageInfo(String str, Context context) {
        try {
            return getPackageManager(context).getPackageInfo(str, 4096);
        } catch (Exception unused) {
            return null;
        }
    }

    private static PackageManager getPackageManager(Context context) {
        return context.getPackageManager();
    }

    public static String getDataDir(String str, Context context) {
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return applicationInfo.dataDir;
    }

    public static String getInstalledDate(String str, Context context) {
        DateFormat dateTimeInstance = DateFormat.getDateTimeInstance();
        PackageInfo packageInfo = getPackageInfo(str, context);
        Objects.requireNonNull(packageInfo);
        return dateTimeInstance.format(Long.valueOf(packageInfo.firstInstallTime));
    }

    public static String getNativeLibDir(String str, Context context) {
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return applicationInfo.nativeLibraryDir;
    }

    public static String getParentDir(String str, Context context) {
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        File parentFile = new File(applicationInfo.sourceDir).getParentFile();
        Objects.requireNonNull(parentFile);
        return parentFile.toString();
    }

    public static String getSourceDir(String str, Context context) {
        ApplicationInfo applicationInfo = getApplicationInfo(str, context);
        Objects.requireNonNull(applicationInfo);
        return applicationInfo.sourceDir;
    }

    public static String getUpdatedDate(String str, Context context) {
        DateFormat dateTimeInstance = DateFormat.getDateTimeInstance();
        PackageInfo packageInfo = getPackageInfo(str, context);
        Objects.requireNonNull(packageInfo);
        return dateTimeInstance.format(Long.valueOf(packageInfo.lastUpdateTime));
    }

    public static String getPackageName(String str, Context context) {
        if (getPackageInfo(str, context) == null) {
            return null;
        }
        PackageInfo packageInfo = getPackageInfo(str, context);
        Objects.requireNonNull(packageInfo);
        return packageInfo.packageName;
    }

    public static String getVersionName(String str, Context context) {
        if (getPackageInfo(str, context) == null) {
            return null;
        }
        PackageInfo packageInfo = getPackageInfo(str, context);
        Objects.requireNonNull(packageInfo);
        return packageInfo.versionName;
    }
}
